using System;
using System.Web.Mvc;
using RMH.Entities;
using RMH.Services;
using RMH.Web.Models.Binders;

namespace RMH.Web.Controllers
{
    public class SupportPersonController : ControllerBase
    {
        private ISupportPersonService supportPersonService;

        public SupportPersonController(ISupportPersonService supportPersonService)
        {
            this.supportPersonService = supportPersonService;
        }

        //
        // GET: /SupportPerson/
        [Authorize(Roles = "Administrator,Volunteer")]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult DropDown(string firstName, string lastName)
        {
            var people = supportPersonService.Search(firstName, lastName);
            return View("_SupportPersonDropDown", people);
        }

        //
        // GET: /SupportPerson/Details/5

        [Authorize(Roles = "Administrator,Volunteer")]
        public ActionResult Details(int? id)
        {
            return View(supportPersonService.Get(id.Value));
        }

        //
        // GET: /SupportPerson/Edit/5

        [Authorize(Roles = "Administrator,Volunteer")]
        public ActionResult Edit(int? id)
        {
            SupportPerson supportPerson = new SupportPerson();

            if (!id.HasValue)
            {
            }
            else
            {
                supportPerson = supportPersonService.Get(id.Value);
            }
            

            return View(supportPerson);
        }

        //
        // POST: /SupportPerson/Edit/5

        [Authorize(Roles = "Administrator,Volunteer")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit([ModelBinder(typeof(SupportPersonBinder))] SupportPerson supportPerson)
        {
            if (String.IsNullOrEmpty(supportPerson.FirstName.Trim()))
            {
                ModelState.AddModelError("FirstName", "First Name is required.");
                ModelState.SetModelValue("FirstName", ValueProvider["FirstName"]);
            }
            if (String.IsNullOrEmpty(supportPerson.LastName.Trim()))
            {
                ModelState.AddModelError("LastName", "Last Name is required.");
                ModelState.SetModelValue("LastName", ValueProvider["LastName"]);
            }
            if (supportPerson.Address == null || String.IsNullOrEmpty(supportPerson.Address.Street1))
            {
                ModelState.AddModelError("AddressStreet1", "Address Street is required.");
                ModelState.SetModelValue("AddressStreet1", ValueProvider["AddressStreet1"]);
            }
            if (supportPerson.Address == null || String.IsNullOrEmpty(supportPerson.Address.City))
            {
                ModelState.AddModelError("AddressCity", "Address City is required.");
                ModelState.SetModelValue("AddressCity", ValueProvider["AddressCity"]);
            }
            if (supportPerson.Address == null || String.IsNullOrEmpty(supportPerson.Address.State))
            {
                ModelState.AddModelError("AddressState", "Address State is required.");
                ModelState.SetModelValue("AddressState", ValueProvider["AddressState"]);
            }
            if (supportPerson.Address == null || String.IsNullOrEmpty(supportPerson.Address.PostalCode))
            {
                ModelState.AddModelError("AddressPostalCode", "Address Postal Code is required.");
                ModelState.SetModelValue("AddressPostalCode", ValueProvider["AddressPostalCode"]);
            }
            else if (supportPerson.Address.PostalCode.Trim().Length < 5)
            {
                ModelState.AddModelError("AddressPostalCode", "Address Postal Code is invalid.");
                ModelState.SetModelValue("AddressPostalCode", ValueProvider["AddressPostalCode"]);
            }
            if (!ModelState.IsValid)
            {
                supportPersonService.Evict(supportPerson);
                return View(supportPerson);
            }
            supportPersonService.Save(supportPerson);
            return RedirectToAction("Index");
        }

        [Authorize(Roles = "Administrator,Volunteer")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ViewResult List(string firstName, string lastName)
        {
            return View(supportPersonService.Search(firstName, lastName));
        }
    }
}
